<template>
    <div class="items">
      <div class="handle">
        <el-button @click="insertPic">新增照片墙</el-button>
      </div>
      <el-table :data="tableData" style="width: 100%">
        <!-- <el-table-column prop="name" label="子标题">
          <template slot-scope="scope">
            <el-input
              placeholder="子标题"
              v-model="scope.row.name"
              style="width: 100%"
            ></el-input>
          </template>
        </el-table-column> -->
        <el-table-column prop="weight" label="权重">
          <template slot-scope="scope">
            <el-input
              placeholder="权重"
              v-model="scope.row.weight"
              style="width: 100%"
            ></el-input>
          </template>
        </el-table-column>
        <!-- <el-table-column prop="linkUrl" label="链接">
          <template slot-scope="scope">
            <el-input
              placeholder="链接"
              v-model="scope.row.linkUrl"
              style="width: 100%"
            ></el-input>
          </template>
        </el-table-column> -->
        <el-table-column prop="weight" label="图片" >
          <template slot-scope="scope" >
              <uploadCropper
              style="height: 160px"
                :handleAvatarSuccess="handlePicSuccess(scope.$index)"
                :deletePic="() => (scope.row.imgUrl = '')"
                :url="scope.row.imgUrl"
              />
          </template>
        </el-table-column>
        <el-table-column label="操作" >
          <template slot-scope="scope" >
            <el-button type="text" @click="saveItem(scope.row, scope.$index)">{{ scope.row.isNew ? '保存' : '更新'}}</el-button>
            <el-button type="text" @click="delItem(scope.row, scope.$index)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <div class="paginationCommon">
        <el-pagination
          background
          :page-size="limit"
          :total="total"
          @current-change="getList"
          layout="prev, pager, next"
        >
        </el-pagination>
      </div>
    </div>
</template>

<script>
export default {
  props: ["originForm2", "isEdit", "closeEditModal", "zlId"],
  data () {
    return {
      limit: 5,
      total: 0,
      tableData: []
    }
  },
  methods: {
    insertPic () {
      if (this.tableData.some(item => item.isNew)) {
        this.$message.info('请先保存新增的条目~')
        return
      }
      this.tableData.unshift({
        name: '新增',
        weight: '1',
        imgUrl: '',
        linkUrl: '',
        isNew: true
      })
    },
    handlePicSuccess(index, res, file) {
      const $index = index;
      return (res, file) => {
        const fileObj = this.$utils.judgeLogin(res);
        this.tableData[$index].imgUrl = fileObj.url;
        this.tableData = JSON.parse(JSON.stringify(this.tableData));
        console.log(this.tableData)
      };
    },
    saveItem(row, index) {
      const ajaxType = !row.isNew ? "put" : "post";
      const url = row.isNew
        ? `/api/zl/title/${this.originForm2.uuid}/item.json`
        : `/api/zl/title/item/${row.uuid}.json`;
      row.sourceType = 2
      this.$axios[ajaxType](url, row).then((response) => {
        this.$message.success("提交成功");
        this.getList();
      });
    },
    delItem(row, index) {
      if(row.isNew) {
        this.tableData.splice(index, 1);
        return
      }
      this.$axios['delete'](`/api/zl/title/item/${row.uuid}.json`, row).then((response) => {
        this.$message.success("提交成功");
        this.getList();
      });
    },
    getList(page = 1) {
      const { formInline, limit } = this;
      const start = (page - 1) * limit || 0;;
      const params = {
        start,
        limit
      };
      this.$axios.get(`/api/zl/title/${this.originForm2.uuid}/item.json`, {params}).then((response) => {
        this.tableData = response.list;
        this.total = response.count;
      });
    }
  },
  created() {
    this.getList()
  }
}
</script>

<style>

</style>